//
//  ApprovalViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/18.
//  Copyright © 2017年 test. All rights reserved.
//

#import "ApprovalViewController.h"




@interface ApprovalViewController ()<LynnTypeButtonActionDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *approvalTableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation ApprovalViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.barTitleLable.text = @"主任审批";
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    [self initUI];
    [self getData];
}
- (void)initUI{
    LynnTypeButtonView  *typeBtnView = [[LynnTypeButtonView  alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    [typeBtnView setTypeButtonTitles:@[@"收案审批",@"结案审批"] withDownLableHeight:2 andDeleagte:self];
    [typeBtnView setTypeButtonNormalColor:[UIColor blackColor] andSelectColor:[UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
    typeBtnView.backgroundColor = [UIColor whiteColor];
    [typeBtnView setSelectButtonIndex:self.typeButtonIndex];
    [self.view insertSubview:typeBtnView atIndex:0];
    [self.view insertSubview:self.approvalTableView belowSubview:typeBtnView];
    
}
- (void)getData{
    
    if (self.typeButtonIndex == 0) {
        
        [NetworkingManager getUrl:[NSString stringWithFormat:@"%@case_state=2&company_id=%@",APPROVEURL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData){
            self.dataSource = responseData[@"result"];
            if (self.dataSource.count == 0) {
                [MBProgressHUD showSuccess:@"暂无更多数据"];
            }
            NSLog(@"approvalData = %@",self.dataSource);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.approvalTableView reloadData];
            [self.approvalTableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.approvalTableView.mj_header endRefreshing];
        }];
    }else if (self.typeButtonIndex == 1){
        [NetworkingManager getUrl:[NSString stringWithFormat:@"%@case_state=5&company_id=%@",APPROVEURL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData){
            self.dataSource = responseData[@"result"];
            if (self.dataSource.count == 0) {
                [MBProgressHUD showSuccess:@"暂无更多数据"];
            }
            NSLog(@"approvalData = %@",self.dataSource);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.approvalTableView reloadData];
            [self.approvalTableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.approvalTableView.mj_header endRefreshing];
        }];
    }
    
}
- (void)loadNewData{
    [self.dataSource removeAllObjects];
    [self getData];
}
#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index
{
    NSLog(@"现在点击的是%ld个",index);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.typeButtonIndex = index;
    self.dataSource = nil;
    [self.approvalTableView reloadData];
    [self getData];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ApprovalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApprovalCell" forIndexPath:indexPath];
    cell.titleLable.text = self.dataSource[indexPath.row][@"ay"];
    cell.caseNumLable.text = [NSString stringWithFormat:@"案号： %@",self.dataSource[indexPath.row][@"ah_number"]];
    cell.bailorLable.text = [NSString stringWithFormat:@"委托人： %@",self.dataSource[indexPath.row][@"wtr"]];
    cell.adversaryLable.text = [NSString stringWithFormat:@"对方当事人： %@",self.dataSource[indexPath.row][@"dfdsr"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CaseDetailViewController *CDVC = [[CaseDetailViewController alloc] init];
    CDVC.caseDetailDic = self.dataSource[indexPath.row];
    if (self.typeButtonIndex == 0) {
        CDVC.caseDetailFlag = 4;
    }
    else if (self.typeButtonIndex == 1){
        CDVC.caseDetailFlag = 6;
    }
    [self.navigationController pushViewController:CDVC animated:YES];
}
#pragma mark - Getter

- (UITableView *)approvalTableView{
    if (!_approvalTableView) {
        _approvalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, self.view.bounds.size.width, self.view.bounds.size.height - 120) style:UITableViewStylePlain];
        _approvalTableView.backgroundColor = [UIColor clearColor];
        _approvalTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _approvalTableView.delegate = self;
        _approvalTableView.dataSource = self;
        _approvalTableView.estimatedRowHeight = 180;
        [_approvalTableView registerClass:[ApprovalCell class] forCellReuseIdentifier:@"ApprovalCell"];
        _approvalTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
    }
    return _approvalTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
